﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using BLL;
using DAL;
using DAL.Dao;
using DataProj.Areas.BackSys.ServiceTools;
using Model;
using Newtonsoft.Json;
namespace DataProj.Areas.BackSys.Controllers
{
    public class ManagerController : BaseController
    {
        // GET: Manager
        private static ManagerImpl _impl;
        //


        // GET: /BackSys/Manager/
        //public new ActionResult Index()
        //{
        //    return PartialView(_impl.GetList());
        //    if (string.IsNullOrEmpty(MySession.GetSession("managerRows")))
        //    {
        //        MySession.WriteSession("managerRows", 10);
        //    }
        //    if (string.IsNullOrEmpty(MySession.GetSession("managerPage")))
        //    {
        //        MySession.WriteSession("managerPage", 1);
        //    }


        //}
        public ManagerController()
        {
            if (_impl == null) _impl = new ManagerImpl();
            if (string.IsNullOrEmpty(MySession.GetSession("managerRows")))
            {
                MySession.WriteSession("managerRows", 10);
            }
            if (string.IsNullOrEmpty(MySession.GetSession("managerPage")))
            {
                MySession.WriteSession("managerPage", 1);
            }
        }

        public ActionResult ManagerRegister()
        {
            
                switch (LoginType)
                {
                    case "用户":
                        return RedirectToAction("Index","Home");
                    case "管理员":
                    case "快递员":
                        return PartialView(_impl.GetList());
                    default:
                        break;
                }
            
            return PartialView(_impl.GetList());
        }


        public ActionResult ManagerUpdate()
        {
            return PartialView();
        }
        public ActionResult ManagerManage()
        {

            (var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("managerRows")), Convert.ToInt16(MySession.GetSession("managerPage")));
            ViewBag.ManagerList = data;
            MySession.WriteSession("managerTotalPages", totalPages);
            return PartialView();
        }


        //
        // GET: /BackSys/Manager/Create

        public ActionResult Create()
        {
            return PartialView();
        }

        //
        // POST: /BackSys/Manager/Create
        [HttpPost]
        public ActionResult GetItemByID()
        {
            var tool = new JavaScriptSerializer();
            int id = Convert.ToInt16(Request["ID"].Trim()) ;
            string action = Request["Action"];
   
            //string get = MyRequest.GetQueryString();
            //Task obj = tool.DeserializeObject(get) as Task;
            var result = _impl.GetItemByID(Convert.ToInt32(id));
            string seri = tool.Serialize(result);
            return Json(seri);
        }


        [HttpPost]
        public ActionResult Create(Manager manager)
        {
            if (ModelState.IsValid)
            {
                _impl.Add(manager);

                return RedirectToAction("Index");
            }

            return PartialView(manager);
        }
        [HttpPost]
        public ActionResult Add()
        {
            Manager manager = new Manager();
            manager.UserName = Request["UserName"];
            manager.UserPass = Request["UserPass"];
            manager.Phone = Request["Phone"];
            manager.TrueName = Request["TrueName"];
            manager.IDCard = Request["IDCard"];
            manager.AddTime = DateTime.Now;
            if (_impl.Add(manager) == true) return Json(1);
            return Json(0);
        }



        [HttpPost]
        public ActionResult ChangePage()
        {
            IDictionary<string, string> set = new Dictionary<string, string>();
            int page = Convert.ToInt16(Request["pageNum"].Trim());
            //MySession.WriteSession("managerPage",page);
            (var totalPages, var data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("managerRows")), page);
            //MySession.WriteSession("managerTotalPages", totalPages);
            //return Json(JsonConvert.SerializeObject(data));
            set.Add("totalPages", totalPages.ToString());
            set.Add("data", JsonConvert.SerializeObject(data));
            return Json(JsonConvert.SerializeObject(set));
        }


        [HttpPost]
        public ActionResult Update()
        {
            Manager manager = new Manager();
            manager.ID = Convert.ToInt32(Request["ID"]);
            manager.UserName = Request["UserName"];
            manager.UserPass = Request["UserPass"];
            manager.Phone = Request["Phone"];
            manager.TrueName = Request["TrueName"];
            manager.IDCard = Request["IDCard"];
            manager.UpdateTime = DateTime.Now;
            if (_impl.Update(manager.ID, manager)) return Json(1);

            return Json(0);
        }
        //
        // GET: /BackSys/Manager/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Manager manager = _impl.Find(id);
            if (manager == null)
            {
                return HttpNotFound();
            }
            return PartialView(manager);
        }

        //
        // POST: /BackSys/Manager/Edit/5

        [HttpPost]
        public ActionResult Delete()
        {
            int id = 0;
            if (int.TryParse(Request["ID"], out id)&& _impl.Delete(id) == true) return Json(1);
            return Json(0);
        }
        protected override void Dispose(bool disposing)
        {
            _impl.Dispose();
            base.Dispose(disposing);
        }
    }
}